package com.example.commons.core

import cn.hutool.core.io.resource.ResourceUtil
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.Json

@kotlinx.serialization.Serializable
data class AreaCode(
    var city: String? = null,
    var code: String? = null,
    var children: List<AreaCode> = emptyList(),
) {
    companion object {

        private var _areaCodes: List<AreaCode>? = null

        @JvmStatic
        val areaCodes: List<AreaCode>
            get() {
                if (_areaCodes == null) {
//                    val codeJson = ClassLoader.getSystemResourceAsStream("static/id_card_area_code.json")
                    val codeJson = ResourceUtil.getStream("static/id_card_area_code.json")
                    _areaCodes = if (codeJson != null) {
                        Json.decodeFromString(ListSerializer(serializer()), String(codeJson.readBytes()))
                    } else {
                        emptyList()
                    }
                }
                return _areaCodes ?: emptyList()
            }
    }
}
